/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  install_signal.c
 *    Description:  This file is used to install signals.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/
 
#include "install_signal.h"


void handler(int sig)
{
    switch(sig)
    {
        case SIGINT:
        {
            log_warn("Process captured SIGINT signal!\n");
            signal_global_run = 0;
            break;
        }
        case SIGTERM:
        {
            log_warn("Process captured SIGTERM signal!\n");
            signal_global_run = 0;
            break;
        }
        case SIGSEGV:
        {
            log_warn("Process captured SIGSEGV signal!\n");
            signal_global_run = 0;
            break;
        }
        case SIGPIPE:
        {
            log_warn("Process captured SIGPIPE signal!\n");
            signal_global_run = 0;
            break;
        }
        default:
            break;
    }

    return ;
}

void install_signal(void)
{
    struct sigaction sigact;

    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigact.sa_handler = handler;

    sigaction(SIGINT, &sigact, 0);
    sigaction(SIGTERM, &sigact, 0);
    sigaction(SIGPIPE, &sigact, 0);
    sigaction(SIGSEGV, &sigact, 0);

    return ;
}